﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsBase;
using UtilsCache;
using UtilsORM;
using UtilsORM.MySqlData;

namespace GatewayServerService.Data
{
    public class DBIotDeviceGateWayCache
    {




        /// <summary>
        /// 缓存增加 获取变量列表
        /// </summary>
        /// <param name="cguid"></param>
        /// <returns></returns>
        public iot_device_gateway getGateWayIsOnlineByCache(string dguid)
        {
            iot_device_gateway model = UtilMemoryCacheHelper.get<iot_device_gateway>("getGateWayIsOnlineByCache" + dguid);
            if (model == null)
            {
                EFYihongIotContext ef = new EFYihongIotContext();
                model = ef.iot_device_gateway.Where(c => c.gw_d_guid == dguid && c.gw_live_time > DateTime.Now.AddMinutes(-1)).FirstOrDefault();
                if (model != null)
                {
                    UtilMemoryCacheHelper.set("getGateWayIsOnlineByCache" + dguid, model, 1);
                }
                ef.Dispose();
            }

            return model;
        }
        public iot_device_gateway getGateWayByCache(string dguid)
        {
            iot_device_gateway model = UtilMemoryCacheHelper.get<iot_device_gateway>("iot_device_gateway" + dguid);
            if (model == null)
            {
                EFYihongIotContext ef = new EFYihongIotContext();
                model = ef.iot_device_gateway.Where(c => c.gw_d_guid == dguid).FirstOrDefault();
                if (model != null)
                {
                    UtilMemoryCacheHelper.set("iot_device_gateway" + dguid, model);
                }
                ef.Dispose();
            }

            return model;
        }
    }
}
